! Utility functions and derivatives
module utilityfs

use params
use states

implicit none

contains

!*****************************
real(prec) function get_ck(cons,leis)
	real(prec) cons,leis
	real(prec) bigterm
	real(prec) raised
	raised=1d0/(-gamma)
	bigterm = (1-mweight+mweight*altruism/mweight)
	get_ck=cons/(bigterm**raised+1d0) 
end function get_ck

real(prec) function get_cp(cons, ck)
	real(prec) cons, ck
	get_cp = cons-ck
end function get_cp

real(prec) function get_leisure(work,fam)
	real(prec) work,fam
    get_leisure = Tyear - work - fam 
end function get_leisure
!*****************************

!*****************************
double precision function utility_married(cons,leisure,famcare,pertype,medicaid)
	double precision cons
	double precision leisure
	integer famcare
	integer pertype
	integer medicaid
		
	if (pertype.eq.1)  then !parent

		if (health.ne.1 .and. famcare==0) then !health=1 is healthy
    		utility_married = yrsinpd*((cons/yrsinpd)**(1.d0-gamma)/(1.d0-gamma) + &
										lshifter*(Tyear/yrsinpd)**(1.d0-alpha)/(1.d0-alpha) + &
  					   					formalpref) !if on medicaid then medicaid=1
			if (medicaid==1) then
				utility_married = utility_married + yrsinpd*mcaidpref
			endif
		else
			utility_married = yrsinpd*((cons/yrsinpd)**(1.d0-gamma)/(1.d0-gamma) + &
									   lshifter*(Tyear/yrsinpd)**(1.d0-alpha)/(1.d0-alpha))
		endif

	else !kid

    	utility_married = yrsinpd*((cons/yrsinpd)**(1.d0-gamma)/(1.d0-gamma) + & 
								   lshifter*(leisure/yrsinpd)**(1.d0-alpha)/(1.d0-alpha))

	end if		

end function utility_married

double precision function utility_divorced(cons,leisure,pertype,medicaid)
	double precision cons
	double precision leisure
	integer pertype 
	integer medicaid


	if (pertype.eq.1)  then !parent
		if (health.ne.1) then
		utility_divorced = yrsinpd*((cons/yrsinpd)**(1.d0-gamma)/(1.d0-gamma) + & 
							        lshifter*(Tyear/yrsinpd)**(1.d0-alpha)/(1.d0-alpha) + &
  					   					formalpref) 										
			if (medicaid==1) then
				utility_divorced = utility_divorced + yrsinpd*mcaidpref
			endif
	  	else
		utility_divorced = yrsinpd*((cons/yrsinpd)**(1.d0-gamma)/(1.d0-gamma) + &
									lshifter*(Tyear/yrsinpd)**(1.d0-alpha)/(1.d0-alpha))
		endif
	else !kid
		utility_divorced = yrsinpd*((cons/yrsinpd)**(1.d0-gamma)/(1.d0-gamma) + &
									lshifter*(leisure/yrsinpd)**(1.d0-alpha)/(1.d0-alpha) + &
    											stigma)
	end if	

end function utility_divorced
!*****************************

double precision function utility_deadparent(cons,leisure)
	
	double precision cons
	double precision leisure
	
	utility_deadparent = yrsinpd*((cons/yrsinpd)**(1.d0-gamma)/(1.d0-gamma) + &
								  lshifter*(leisure/yrsinpd)**(1.d0-alpha)/(1.d0-alpha))

end function utility_deadparent
!*****************************

!*****************************
!terminal value function of children
real(prec) function utilitykterminal(totalassets,phitype)
	real(prec) totalassets
	real(prec) c
	real(prec) tremain
	integer phitype
	tremain=25.0
  c=totalassets/tremain
  c=max(c,cmink)
  if (phitype==1) then
	utilitykterminal = (termphiL/(1.0-termphiL))**gammab*(totalassets)**(1d0-gammab)/(1d0-gammab)
  else
	utilitykterminal = (termphiH/(1.0-termphiH))**gammab*(totalassets)**(1d0-gammab)/(1d0-gammab)
  endif
end function utilitykterminal
!*****************************

!*****************************


end module utilityfs
